﻿using CBS.Common;
using CBS.DB.CBSEntity;
using CBS.DB;
using CBS.Model.ManageModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CBS.APP
{
    public class ClassApp
    {
        public static bool CreateClass(ClassViewModel newClass)
        {
            using (CBSContext cbs = new CBSContext())
            {
                cbs.cbs_class.Add(new cbs_class
                {
                    ClassName = newClass.ClassName,
                });

                return cbs.SaveChanges() > 0;
            }
        }

        public static List<ClassViewModel> GetList(Pagination page, string? keyword)
        {
            if (string.IsNullOrWhiteSpace(keyword))
            {
                return GetList(null, page);
            }
            else
            {
                return GetList(t => t.ClassName.Contains(keyword), page);
            }

        }

        public static List<ClassViewModel> GetList(Expression<Func<cbs_class, bool>>? predicate, Pagination page)
        {
            List<cbs_class> list;
            RepositoryBase<cbs_class> repo = new RepositoryBase<cbs_class>();
            if (predicate == null)
            {
                list = repo.FindList(page);
            }
            else
            {
                list = repo.FindList(predicate, page);
            }


            return PropertyHelper.Assignment<ClassViewModel, cbs_class>(list);
        }

        public static List<ClassViewModel> GetSelectList()
        {
            CBSContext cbs = new();
            var data = (from cla in cbs.cbs_class
                       select new ClassViewModel
                       {
                           ClassId = cla.ClassId,
                           ClassName = cla.ClassName
                       }).ToList();
            if (data != null)
            {
                return data;
            }

            else
            {
                return new List<ClassViewModel>();
            }
        }

        public static List<ClassViewModel> GetSelectList(int semesterId)
        {
            CBSContext cbs = new();
            return (from course in cbs.cbs_course_plan
                    join cla in cbs.cbs_class on course.ClassId equals cla.ClassId
                    where course.SemesterId == semesterId
                    select new ClassViewModel
                    {
                        ClassId = cla.ClassId,
                        ClassName = cla.ClassName
                    }).ToList();
        }

        public static bool DeleteClass(int classId, out string msg)
        {
            msg = "";
            using (CBSContext cbs = new CBSContext())
            {
                if (cbs.cbs_course_plan.Any(t => t.ClassId == classId)) 
                {
                    msg = "班级已经被使用，不能删除。";
                    return false;
                }
                cbs.cbs_class.Remove(cbs.cbs_class.First(t => t.ClassId == classId));
                
                return cbs.SaveChanges() > 0;
            }
        }

        public static bool BatchDeleteClass(string key)
        {
            using (CBSContext cbs = new CBSContext())
            {
                cbs.cbs_class.RemoveRange(cbs.cbs_class.Where(t => t.ClassName.Contains(key)));
                return cbs.SaveChanges() > 0;
            }
        }

        public static ClassViewModel GetForm(int classId)
        {
            using (CBSContext cbs = new CBSContext())
            {
                var entity = cbs.cbs_class.FirstOrDefault(t => t.ClassId == classId);

                return PropertyHelper.Assignment<ClassViewModel, cbs_class>(entity);
            }
        }
    }
}
